package com.zxjbyte.yiyi.framework.common.util;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.net.URLEncodeUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.zxjbyte.yiyi.framework.common.core.pdf.PdfHelper;
import com.zxjbyte.yiyi.framework.common.core.pdf.support.ParagraphSupport;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.Objects;

/**
 * @Author zhangxingjia
 * @Date 2023/3/23 16:59
 * @Version: 1.0
 */
@UtilityClass
public class PdfUtil {


    /**
     * 创建简单的文本pdf带水印
     * @param fileOutPath
     * @param title
     * @param context
     */
    public void createSimplePdf(String fileOutPath, String title, String context, PdfPageEventHelper watermarkEvent){
        PdfUtil._simplePdf(FileUtil.getOutputStream(fileOutPath), title, context, watermarkEvent);
    }

    /**
     * 创建简单的文本pdf
     * @param fileOutPath
     * @param title
     * @param context
     */
    public void createSimplePdf(String fileOutPath, String title, String context){
        PdfUtil.createSimplePdf(fileOutPath, title, context, null);
    }

    /**
     * 创建简单的文本pdf写出到浏览器下载带水印
     * @param response
     * @param title
     * @param context
     */
    @SneakyThrows
    public void writeSimplePdf(HttpServletResponse response, String filename, String title, String context, PdfPageEventHelper watermarkEvent){
        response.setContentType("application/pdf");
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename*=utf-8''" + URLEncodeUtil.encode(filename));
        PdfUtil._simplePdf(response.getOutputStream(), title, context, watermarkEvent);
    }

    /**
     * 创建简单的文本pdf写出到浏览器下载
     * @param response
     * @param title
     * @param context
     */
    public void writeSimplePdf(HttpServletResponse response, String filename, String title, String context){
        PdfUtil.writeSimplePdf(response, filename, title, context, null);
    }



    /**
     * 简单的文本pdf带水印
     * @param os
     * @param title
     * @param context
     * @param watermarkEvent
     */
    @SneakyThrows
    private void _simplePdf(OutputStream os, String title, String context, PdfPageEventHelper watermarkEvent){
        // 创建文档
        Document document = PdfHelper.getDocument();
        try {
            // 创建输出流指定文件名字
            PdfWriter writer = PdfWriter.getInstance(document, os);
            writer.setPdfVersion(PdfWriter.VERSION_1_6);

            // PDF文档属性，可使用Google浏览器打开点击属性查看
            document.addTitle(title);
            document.addAuthor("yiyi-boot");
            document.addSubject(title);
            // 开始写入 PDF 文档内容
            document.open();
            // 添加水印
            if(Objects.nonNull(watermarkEvent)){
                writer.setPageEvent(watermarkEvent);
            }
            ParagraphSupport pSupport = ParagraphSupport.getInstance();
            // 添加标题
            document.add(pSupport.createTitle(title));
            // 内容
            document.add(pSupport.createText(context));
        } finally {
            // 关闭输出流并完成文档的写入
            document.close();
        }
    }

}
